package cn.jekun.shop.common.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import cn.jekun.component.base.validator.annotation.Security;
import cn.jekun.component.core.bean.gen.ErpShop;
import cn.jekun.component.core.bean.gen.ErpShoptype;
import cn.jekun.component.core.service.gen.ErpShoptypeService;

@Service(ShopTypeService.BEAN_NAME)
public class ShopTypeService extends ShopBaseService{
	public static final String BEAN_NAME = "cn.jekun.shop.common.service.ShopTypeService";
	public static final String NAMESPACE = "SHOP_SHOPTYPE";
	@Resource(name=ErpShoptypeService.BEAN_NAME)
	ErpShoptypeService erpShoptypeService;
	@Override
	public String getNamespace() {
		return NAMESPACE;
	}
	//通过 parentID 获取商店信息
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public JSONObject showShopTypes(
			@Security
			String parentId){
		JSONObject jsonObject = new JSONObject();
		Map formMap = new HashMap();
		List<ErpShoptype> list = new ArrayList();
		if(StringUtils.isNotBlank(parentId)){
			formMap.put("parentId", parentId);
		}
		list = this.list(formMap);
		if(list == null){
			return executeRerutnFail(jsonObject);
		}
		ErpShoptype shopType = new ErpShoptype();
		shopType.setShopTypeId(parentId);
		shopType.setShopTypeName("全部");
		list.add(0, shopType);
		jsonObject.put("shopTypeList", list);
		return executeRerutn(jsonObject);
	}
	

	//通过 搜索地址  address 获取商店信息    7月28号加入
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public JSONObject showShops(
			@Security
			String address){
		JSONObject jsonObject = new JSONObject();
		Map formMap = new HashMap();
		List<ErpShop> list = new ArrayList();
		if(StringUtils.isNotBlank(address)){
			formMap.put("address", address);
		}
		list = this.list(formMap);
		if(list == null){
			return executeRerutnFail(jsonObject);
		}
		ErpShop shop = new ErpShop();
		shop.setAddress(address);
		list.add(0, shop);
		jsonObject.put("shopList", list);
		return executeRerutn(jsonObject);
	}
	



	
	public JSONObject showAllShopTypes(){
		JSONObject jsonObject = new JSONObject();
		Map formMap = new HashMap();
		List<ErpShoptype> list = new ArrayList();
		list = this.list(this.getStatementName("all"),formMap);
		
		if(list == null){
			return executeRerutnFail(jsonObject);
		}
		
		List listData=new ArrayList();
		
		HashMap<String, Object> hashMap;
		
		for(int i=0;i<list.size();i++){
			
			if(StringUtils.isBlank(list.get(i).getParentId())){
				hashMap=new HashMap<String, Object>();
				hashMap.put("mcc", list.get(i).getMcc());
				hashMap.put("memo", list.get(i).getMemo());
				hashMap.put("parentId", list.get(i).getParentId());
				hashMap.put("shopTypeId", list.get(i).getShopTypeId());
				hashMap.put("shopTypeName", list.get(i).getShopTypeName());
				hashMap.put("sort", list.get(i).getSort());
				hashMap.put("childTypeList", getChildType(list.get(i).getShopTypeId(),list));
				listData.add(hashMap);
			}
			
		}
		jsonObject.put("shopTypeList", listData);
		
		return executeRerutn(jsonObject);
	}
	
	
	public JSONArray getChildType(String parenrId,List<ErpShoptype> list){
		
		JSONArray jsonArray=new JSONArray();
		
		
		for(int i=0;i<list.size();i++){
			
			if(parenrId.equals(list.get(i).getParentId())){
					
					JSONObject jsonObject=new JSONObject();

					jsonObject.put("mcc", list.get(i).getMcc());
					jsonObject.put("memo", list.get(i).getMemo());
					jsonObject.put("parentId", list.get(i).getParentId());
					jsonObject.put("shopTypeId", list.get(i).getShopTypeId());
					
					
					
					jsonObject.put("shopTypeName", list.get(i).getShopTypeName());
					jsonObject.put("sort", list.get(i).getSort());
					
					jsonArray.add(jsonObject);
				//}
			
			}
			
		}
		
		return jsonArray;
		
	}
	
	
	



}
